package com.kreedx.record

import android.Manifest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import com.kreedx.audio.AutioHelper
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    var helper:AutioHelper? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        hello_text.text = "play"

        helper = AutioHelper(applicationContext)

        hello_text.setOnClickListener {
            Toast.makeText(baseContext,"play",Toast.LENGTH_SHORT).show()
            Thread(Runnable {
                helper?.run { getManager().play() }
            }).start()
        }

        start.setOnClickListener {
            Toast.makeText(baseContext,"start",Toast.LENGTH_SHORT).show()
            Thread(Runnable {
                helper?.run { getManager().record() }
            }).start()
        }

        stop.setOnClickListener {

            Toast.makeText(baseContext,"stop",Toast.LENGTH_SHORT).show()
            helper?.run {
                getManager().end()
            }
        }

        val rxPermissions = RxPermissions(this)
        rxPermissions.requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO)
            .subscribe{permission ->
            if (permission.granted) {
                Toast.makeText(baseContext,"用户给权限啦",Toast.LENGTH_SHORT).show()
                return@subscribe
            }
            if (permission.shouldShowRequestPermissionRationale) {
                Toast.makeText(baseContext,"用户不给权限",Toast.LENGTH_SHORT).show()
                return@subscribe
            }
                Toast.makeText(baseContext,"弹窗不显示",Toast.LENGTH_SHORT).show()
        }

    }
}
